package com.laikan.reader.core.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.WindowManager;
import android.widget.TextView;
import com.laikan.library.utils.SPUtil;
import com.laikan.reader.core.bean.Text;

import java.util.List;

/**
 * @author gt127
 */
public class SimpleTextView extends TextView {

    // 计数用的
    public int words = 0;
    Context context;
    String str = null;
    Paint paint = new Paint();
    List<Text> strList = null;
    int text_size = 65;
    String colorStr = "#424542";
    String tailColor = "#38ab25";
    String titleColor = "#333333";

    public SimpleTextView(Context context) {
        super(context);
        this.context = context;
    }

    public SimpleTextView(Context context, String str) {
        super(context);
        this.context = context;
        this.str = str;
    }

    public SimpleTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SimpleTextView(Context context, AttributeSet attrs, String str) {
        super(context, attrs);
        this.context = context;
        this.str = str;
    }

    /**
     * 重新排版
     *
     * @param textSize px, 为0时不改变字号
     */
    @SuppressWarnings ("unused")
    private void refresh(int textSize) {
        if (textSize > 0) {
            this.setText_size(textSize);
        }
        this.postInvalidate();
    }

    public void refresh(String color) {
        if (color != null) {
            this.setColorStr(color);
        }
        this.postInvalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // KLog.e("onDraw", this.text_size + " show text_size " + this.colorStr);
        // 设置字体和颜色, 很重要
        super.onDraw(canvas);
        boolean flag = false;
        init(canvas, paint);
        if (SPUtil.getBoolean("model_flag", false)) {
            titleColor = "#CCCCCC";
        }else {
            titleColor = "#333333";
        }

        Paint paintLine = new Paint();
        paintLine.setStrokeWidth(3);// 笔宽5像素
        paintLine.setColor(Color.parseColor(this.titleColor));
        paint.setColor(Color.parseColor(this.titleColor)); // 设置画笔颜色
        paint.setStrokeWidth((float)4.0);
        WindowManager wm = (WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE);

        int width = wm.getDefaultDisplay().getWidth();
        float lasty = 0;
        float startx = 0;
        boolean flags = false;
        if (strList != null)

        {
            for (Text text : strList) {

                if (text.getcType() != null && text.getcType().getValue() == 3) {
                    paint.setColor(Color.parseColor(this.tailColor));
                    paint.setTextSkewX(-0.3f);
                    paint.setTextSize(SPUtil.getInt("reader_point_size", 65));
                    canvas.drawText(text.getStr(), text.getX(), text.getY(), paint);
                }else if (text.getcType() != null && text.getcType().getValue() == 1) {
                    paint.setColor(Color.parseColor(this.titleColor));
                    paint.setTextSkewX(0);
                    paint.setTextSize(SPUtil.getInt("reader_point_size", 65) + 8);
                    if (!flags) {
                        startx = text.getX();
                        flags = true;
                    }

                    lasty = text.getY() + SPUtil.getInt("reader_point_size", 65) + 15;
                    canvas.drawText(text.getStr(), text.getX(), text.getY() + (SPUtil.getInt("reader_point_size", 65)), paint);
                }else if (text.getcType() == null || text.getcType().getValue() == 2) {
                    paint.setColor(Color.parseColor(this.colorStr));
                    paint.setTextSkewX(0);
                    paint.setTextSize(SPUtil.getInt("reader_point_size", 65));
                    canvas.drawText(text.getStr(), text.getX(), text.getY(), paint);

                }

            }
        }
        if (lasty != 0) {
            canvas.drawLine(startx, lasty + 10, width - startx, lasty + 10, paintLine);
        }

    }

    private void init(Canvas canvas, Paint paint) {
        // 绘制测试用矩形 start
        // paint.setColor(Color.parseColor("#F7F2ED"));
        // canvas.drawRect(paddingLeft, paddingTop, screenWidth - paddingRight,
        // screenHeight - paddingBottom, paint);
        // 绘制矩形 end

        paint.setColor(Color.parseColor(this.colorStr));
        paint.setTextSize(text_size);
        // 抗锯齿,一定要开
        paint.setAntiAlias(true);
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public String getStr() {
        return str;
    }

    // 2014-04-22
    public void setStr(String str) {
        this.str = str;
    }

    public List<Text> getStrList() {
        return this.strList;
    }

    public void setStrList(List<Text> list) {
        this.strList = list;

    }

    // 将一个段落分词, 使字符和标点符号连接在一起
    public int getText_size() {
        return text_size;
    }

    public void setText_size(int text_size) {
        this.text_size = text_size;
    }

    public void setColorStr(String color) {
        this.colorStr = color;
    }
}
